package 代码记录.指定公司代码练习记录.华为_2024.分值200;

import java.util.Scanner;

/**
 * @author zx
 * @create 2024-02-16 14:48
 * 二分查找，感觉自己做不出来；暂时不看
 */
public class 员工派遣_33 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long x = sc.nextLong(), y = sc.nextLong(), cntx = sc.nextLong(), cnty = sc.nextLong();
        System.out.println(findMinimumK(x, y, cntx, cnty));
    }

    public static long findMinimumK(long x, long y, long cntx, long cnty) {
        long l = 1, r = (long) Math.pow(10, 18);
        while (l < r) {
            long mid = l + (r - l) / 2;
            if (isValid(mid, x, y, cntx, cnty)) r = mid;
            else l = mid + 1;
        }
        return l;
    }

    public static boolean isValid(long k, long x, long y, long cntx, long cnty) {
        long a = k / x - k / (x * y), b = k / y - k / (x * y), d = k - a - b;
        return d >= Math.max(0, cntx - b) + Math.max(0, cnty - a);
    }
}
